package com.domain.service.impl;

import com.domain.common.Const;
import com.domain.common.Result;
import com.domain.dao.IndustryMapper;
import com.domain.dao.PersonalLabelMapper;
import com.domain.dao.UsersMapper;
import com.domain.dao.UsersWalletMapper;
import com.domain.pojo.Industry;
import com.domain.pojo.PersonalLabel;
import com.domain.pojo.Users;
import com.domain.pojo.UsersWallet;
import com.domain.service.LoginService;
import com.domain.utils.DateUtils;
import com.domain.utils.NotNullUtils;
import com.domain.utils.UUIDUtils;
import com.domain.utils.wyyx.IMSet;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UsersWalletMapper usersWalletMapper;

    @Autowired
    private PersonalLabelMapper personalLabelMapper;

    @Autowired
    private IndustryMapper industryMapper;

    /**
     * 验证手机号是否被注册
     *
     * @param phone
     * @return
     */
    @Override
    public int checkPhone(String phone) {
        int resultCount = usersMapper.selectByPhone(phone);
        return resultCount;

    }

    /**
     * 注册保存用户信息
     *
     * @param phoneNumber
     * @return
     */
    @Override
    public Users saveUser(String phoneNumber,
                          String name,
                          String gender,
                          String birthday,
                          String city,
                          String industry,
                          String personalLabel,
                          String headImage,
                        /*  String nickName,*/
                          HttpServletRequest request) {
        String uuid = UUIDUtils.getRandomUUID();
        String uuidtoken = UUIDUtils.getRandomUUID();
        Users users = new Users();
        users.setSpareOne(uuidtoken);
        users.setId(uuid);
        if (NotNullUtils.isNotNullObject(phoneNumber)) {
            users.setPhone(phoneNumber);
        }
        if (NotNullUtils.isNotNullObject(headImage)) {
            users.setHeadImage(headImage);
        }
       /* if (NotNullUtils.isNotNullObject(nickName)) {
            users.setNickname(nickName);
        }*/
        if (NotNullUtils.isNotNullObject(name)) {
            users.setName(name);
        }
        if (NotNullUtils.isNotNullObject(gender)) {
            users.setGender(new Integer(gender));
        }
        if (NotNullUtils.isNotNullObject(birthday)) {
            users.setBirthday(DateUtils.strToDate(birthday, "yyyy-MM-dd"));
        }
        if (NotNullUtils.isNotNullObject(city)) {
            users.setCity(city);
        }
        if (NotNullUtils.isNotNullObject(industry)) {
            users.setIndustry(industry);
        }
        if (NotNullUtils.isNotNullObject(personalLabel)) {
            users.setPersonalLabel(personalLabel);
        }
        users.setCreatTime(new Date());
        usersMapper.insertSelective(users);
        //生成用户钱包
        UsersWallet usersWallet = new UsersWallet();
        usersWallet.setId(UUIDUtils.getRandomUUID());
        usersWallet.setUserId(uuid);
        usersWallet.setBalance(new BigDecimal(0.00));
        usersWallet.setCreatTime(new Date());
        usersWalletMapper.insertSelective(usersWallet);
        /*注册创建im账号*/
        try {
            IMSet.createUser(uuid, name, headImage, birthday, phoneNumber, gender, uuidtoken);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return checklogin(phoneNumber, request);
    }

    /**
     * 用户登录验证
     *
     * @param phoneNumber
     * @return
     */
    @Override
    public Users checklogin(String phoneNumber, HttpServletRequest request) {
        Users user = usersMapper.selectByUserPhone(phoneNumber);
        //将用户存在session中
        HttpSession session = request.getSession();
        session.setAttribute(Const.CURRENT_USER, user);
        return user;
    }

    /**
     * 获取个人标签列表
     *
     * @return
     */
    @Override
    public Result getUserLable(String pageNum, String pageSize) {
        PageHelper.startPage(new Integer(pageNum), new Integer(20));
        List<PersonalLabel> personalLabels = personalLabelMapper.selectLists();
        PageInfo pageInfo = new PageInfo(personalLabels);
        return Result.success("操作成功", pageInfo.getList());
    }

    /**
     * 获取行业标签列表
     *
     * @return
     */
    @Override
    public Result getUserIndustrysList(String pageNum, String pageSize) {
        PageHelper.startPage(new Integer(pageNum), new Integer(20));

        List<Industry> industries = industryMapper.selectList();
        PageInfo pageInfo = new PageInfo(industries);
        return Result.success("操作成功", pageInfo.getList());
    }
}


